package com.commerceerp.system.pay;

import java.util.HashMap;
import java.util.Map;

public class WxPayCode {

    //商户appid
    private static final String APPID = "wx3e6f113917889e6c";

    //商家id
    private static final String MCHID = "1389616002";

    //回调地址
    private static final String NOTIFY_URL = "https://jv.mind-erp.com/pay/weixinpay/callback";

    //支付秘钥
    private static final String PAY_KEY = "0d07c119d61046aa416893ebfedb1e99";

    /**
     * 微信扫码支付 模式二
     *
     * @return 返回的就是二维中的值
     * @throws Exception
     */
    public static String getWxPayQRCode(String ip, String tradeno,String needPayMoney,String subject) throws Exception {

        Map<String, String> map = new HashMap<String, String>();
        // 公众账号ID
        map.put("appid", APPID);
        // 商户号
        map.put("mch_id", MCHID);
        // 随机数
        map.put("nonce_str", CommonUtil.getRandomString(20));
        // 商品描述
        map.put("body", subject);
        // 订单号
        map.put("out_trade_no", tradeno);
        // 总金额单位分
        int total_fee = (int)(Double.valueOf(needPayMoney) * 100);
        map.put("total_fee", total_fee+"");
        // ip
        map.put("spbill_create_ip", ip);
        // 通知地址
        map.put("notify_url", NOTIFY_URL);
        // 交易类型
        map.put("trade_type", "NATIVE");
        // 商品ID
        map.put("product_id", "1474548748");
        // 签名
        map.put("sign", QRSign.getSign(map, PAY_KEY));

        // 请求地址及请求数据
        String url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
        String data = CommonUtil.mapToXml(map);
        Map<String, String> resultMap = CommonUtil.httpsRequestToXML(url, "POST", data);
        // 二维码内容
        String qrCode = "";
        if (resultMap.get("return_code").toString().equals("SUCCESS")) {
            qrCode = resultMap.get("code_url").toString();
            System.out.println(qrCode);
        }
        return qrCode;
    }

    public static void main(String[] args) throws Exception {
        getWxPayQRCode("36.7.138.114","44874445445","0.01","微信扫描二维码支付测试");
    }
}
